<?php
class CommentsController extends AppController {

	var $name = 'Comments';
	var $components = array('Site');

	function index() {
  	if ($this->params['webservices'] == 'Rss') {
      $fields=array('Comment.id','Comment.text','Comment.created','Post.id','Post.title','User.login','User.name');
			$this->set('comments', $this->Comment->findAll("Comment.del = 0",$fields,"Comment.created DESC",10,null,0));
			$this->pageTitle='Recent replies';
			$this->render();
			exit;
		}
		list($sort,$order)=$this->Site->getSort('added');
    $count=$this->Comment->findCount("Comment.del = 0");
    $fields=array('Comment.id','Comment.text','Comment.vote','Comment.created','Post.id','Post.title','User.login','User.name');
		$this->set('comments', $this->Comment->findAll("Comment.del = 0",$fields,$sort." ".$order,PAGESIZE,$this->pager($count,PAGESIZE),0));
	}

	function add() {
  	$this->checkSession();
		if(empty($this->data)) {
			exit;
		}

    $cpost=$this->Comment->Post->findById($this->data['Comment']['post_id'],"Post.id,Post.closed",null,-1);
		if(empty($cpost)) {
       $this->redirect('/');
			exit;
		}
		if($cpost['Post']['closed']) {
			$this->Session->setFlash('Topic is closed!','msgerror');
			$this->redirect('/topic/'.$this->data['Comment']['post_id'].'.html');
			exit;
		}

		vendor('func');

		// set level
		if($this->data['Comment']['parent_id']!=0) {
			$pcom=$this->Comment->findById($this->data['Comment']['parent_id'],"Comment.level",null,-1);
			$this->data['Comment']['level']=$pcom['Comment']['level']+1;
		}

		$this->data['Comment']['user_id']=$this->luser['id'];

		if($this->Comment->save($this->data)) {
			$lcid=$this->Comment->getLastInsertId();			
			$this->Comment->Post->inc($this->data['Comment']['post_id'],'replies',1);
			$this->Comment->User->inc($this->data['Comment']['user_id'],'comcount',1);
			if($this->data['Comment']['parent_id']!=0) {
				$this->Comment->inc($this->data['Comment']['parent_id'],'replies',1);
			}
			$this->Session->setFlash('Reply saved.');
			$clink='/topic/'.$this->data['Comment']['post_id'].'.html#c'.$lcid;
               // send notify
			if($this->data['Comment']['parent_id']==0) {
				// send to post author
				$pa=$this->Comment->Post->getauthor($this->data['Comment']['post_id']);
				if($pa['User']['notify'] && $pa['User']['id']!=$this->luser['id']) { // send notify
					$to=$pa['User']['email'];
					$subject="reply to your message notification";
					$message='
You have a new reply to your message:
http://'.$_SERVER['SERVER_NAME'].$clink;
					sendmail($to, $subject, $message, '');
				}
			} else {
				// send to comment author
				$ca=$this->Comment->getauthor($this->data['Comment']['parent_id']);
        if($ca['User']['notify'] && $ca['User']['id']!=$this->luser['id']) { // send notify
        	$to=$ca['User']['email'];
					$subject="reply to your comment notification";
					$message='
You have a new comment to your reply:
http://'.$_SERVER['SERVER_NAME'].$clink;
					sendmail($to, $subject, $message, '');
				}
			}
			//$this->flash('!', $clink);
			$this->redirect($clink);
		} else {
			$this->Session->setFlash('Error!','msgerror');
			$this->redirect('/');
		}
	}

	function form ($postid,$parentid) {
		if(!$this->luser) exit;
		$this->layout="ajax";
		$this->set('postid', $postid);
		$this->set('parentid', $parentid);
	}

	function load ($cid) {
		vendor('filter');
  	$this->layout="ajax";
		$c=$this->Comment->read(array("Comment.text","Comment.del"),$cid);
		if(!$c['Comment']['del'])
			$this->set('ctext', br($c['Comment']['text']));
		else
			$this->set('ctext', "comment deleted");
	}

	function admin_index() {
		$this->checkAdmin(); $this->layout='admin';
		$this->Comment->recursive = 0;
    $count=$this->Comment->findCount();
		list($sort,$order)=$this->Site->getSort('added');
		$this->set('count',$count);
		$this->set('comments', $this->Comment->findAll(null,null,$sort." ".$order,APAGESIZE,$this->pager($count,APAGESIZE),0));
	}

	function admin_delete($id = null) {
		$this->checkAdmin(); $this->layout='admin';
		if(!$id) {
			$this->Session->setFlash('Invalid id for Comment');
			$this->redirect('/admin/comments/');
			exit;
		}
		$this->Comment->recursive = -1;
		$ch=$this->Comment->findByParentId($id);
		if (count($ch['Comment'])) {
			$this->Comment->hide($id);
		} else {
			$c=$this->Comment->read(array('user_id','post_id','parent_id'), $id);
			$this->Comment->del($id);
			$this->Comment->Post->inc($c['Comment']['post_id'],'replies',-1);
			$this->Comment->User->inc($c['Comment']['user_id'],'comcount',-1);
			if($c['Comment']['parent_id']!=0) {
				$this->Comment->inc($c['Comment']['parent_id'],'replies',-1);
			}
		}
		$this->Session->setFlash('Reply deleted: id '.$id.'');
		$this->redirect('/admin/comments/');
	}

	function admin_edit($id = null) {
		$this->checkAdmin(); $this->layout='admin';
		if(empty($this->data)) {
			if(!$id) {
				$this->redirect('/admin/comments/');
				exit;
			}
			$this->Comment->recursive=0;
			$this->data = $this->Comment->read(null, $id);

      vendor("filter");
			$this->data['Comment']['text']=makecode($this->data['Comment']['text'],false);
		} else {
			$this->cleanUpFields();
			if($this->Comment->save($this->data)) {
				$this->Session->setFlash('Reply saved.');
				$this->redirect('/admin/comments/');
			} else {
				$this->Session->setFlash('Fix errors.',"msgerror");
			}
		}
	}

}
?>